# src/knowledge_base/gb_standards.py
import logging

class GBStandardChecker:
    STANDARDS = {
        "GB/T 14665-2023": {
            "linear_dimensions": {
                "min": 10,
                "max": 1000,
                "tolerance": lambda x: x*0.001 if x > 100 else 0.1
            },
            "angular_tolerance": 0.5  # 度
        }
    }

    def check_dimension(self, dimension):
        # 清理输入值
        if isinstance(dimension, str):
            # 使用正则表达式提取数字部分（包括小数和负数）
            import re
            match = re.search(r"[-+]?\d*\.?\d+", dimension)
            if not match:
                logging.error("输入值中未找到有效数字: %s", dimension)
                return {"code": "ERROR", "desc": "无效的尺寸格式"}
            dimension = match.group()

        # 类型转换
        try:
            dimension = float(dimension)
        except (TypeError, ValueError) as e:
            logging.error("尺寸转换失败 | 原始值: %s | 错误: %s", dimension, str(e))
            return {"code": "ERROR", "desc": "无效的尺寸值"}

        # 后续验证逻辑保持不变
        std = self.STANDARDS["GB/T 14665-2023"]
        if dimension < std["linear_dimensions"]["min"]:
            return {"code": "5.2.1", "desc": "尺寸过小"}
        elif dimension > std["linear_dimensions"]["max"]:
            return {"code": "5.2.2", "desc": "尺寸过大"}
        return None
